package main

// #include <stdlib.h>
import "C"

import (
	"unsafe"

	"gitee.com/wenj91/apisix-go-so-plugins/pkg/domain"
	_ "gitee.com/wenj91/apisix-go-so-plugins/pkg/handler/common"
	"gitee.com/wenj91/apisix-go-so-plugins/pkg/intf"

	jsoniter "github.com/json-iterator/go"
)

//export CMD
func CMD(cmd string) *C.char {
	var cm domain.Cmd
	err := jsoniter.Unmarshal([]byte(cmd), &cm)
	if err != nil {
		return C.CString(domain.UnmarshalCmdFailed())
	}

	cmdHandler := intf.Get(cm.Cmd)
	if nil == cmdHandler {
		return C.CString(domain.IlegalCmd(cm.Cmd))
	}

	return C.CString(cmdHandler.Handle(cm.Params))
}

//export Free
func Free(p unsafe.Pointer) {
	C.free(p)
}

func main() {}
